package vn.egame.elockscreen.object;

import android.graphics.Canvas;
import android.graphics.Rect;

public class Animation extends BaseActor{
	private Texture texture;
	private int rows;
	private int columns;
	private int duration;
	private int frameDuration;
	private int currentFrame;
	private long lastTimeDraw;
	private int widthFrame, heightFrame;


	public Animation(Texture texture, int rows, int columns, int frameDuration) {
		super();
		this.texture = texture;
		this.rows = rows;
		this.columns = columns;
		this.frameDuration = frameDuration;
		this.duration = this.frameDuration * columns;
		this.currentFrame = 0;
		widthFrame = texture.getWidth() / columns;
		heightFrame = texture.getHeight() / rows;
		
		setWidth(widthFrame);
		setHeight(heightFrame);

	}

	public void update() {

		if (System.currentTimeMillis() - lastTimeDraw > frameDuration) {

			lastTimeDraw = System.currentTimeMillis();
			currentFrame = ++currentFrame % columns;
		}
	}

	public void onDraw(Canvas canvas, int x, int y) {

		int srcX = currentFrame * widthFrame;

		int srcY = 0 * heightFrame;

		Rect src = new Rect(srcX, srcY, srcX + getWidthFrame(), srcY + getHeightFrame());
		Rect dst = new Rect(x, y, x + getWidthFrame(), y + getHeightFrame());
		
//		Rect src = new Rect(0, 0, getWidthFrame(), getHeightFrame());
//		Rect dst = new Rect(x, y, x + getWidthFrame(), y + getHeightFrame());

		canvas.drawBitmap(texture.getTexture(), src, dst, null);

	}
	
	

	public Texture getTexture() {
		return texture;
	}

	public void setTexture(Texture texture) {
		this.texture = texture;
	}

	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	public int getColumns() {
		return columns;
	}

	public void setColumns(int columns) {
		this.columns = columns;
	}

	public int getDuration() {
		return duration;
	}

	public void setDuration(int duration) {
		this.duration = duration;
	}

	public int getFrameDuration() {
		return frameDuration;
	}

	public void setFrameDuration(int frameDuration) {
		this.frameDuration = frameDuration;
	}

	public int getWidthFrame() {
		return widthFrame;
	}

	public void setWidthFrame(int widthFrame) {
		this.widthFrame = widthFrame;
	}

	public int getHeightFrame() {
		return heightFrame;
	}

	public void setHeightFrame(int heightFrame) {
		this.heightFrame = heightFrame;
	}

}
